package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.AdministrativeDivision;
import com.innovations.inn.service.IAdministrativeDivisionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 行政区划Controller
 *
 * @author dp
 * @date 2024-01-16
 */
@Api(tags = {"行政区划管理"})
@RestController
@RequestMapping("/task/division")
public class AdministrativeDivisionController extends BaseController {
    @Autowired
    private IAdministrativeDivisionService administrativeDivisionService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询行政区划列表
     */
    //@PreAuthorize("@ss.hasPermi('system:division:list')")
    @ApiOperation(value = "行政区划列表", notes = "查询行政区划列表详情", tags = {"行政区划管理"})
    @GetMapping("/list")
    public TableDataInfo list(AdministrativeDivision administrativeDivision) {
        startPage();
        List<AdministrativeDivision> list = administrativeDivisionService.selectAdministrativeDivisionList(administrativeDivision);
        return getDataTable(list);
    }

    /**
     * 导出行政区划列表
     */
    //@PreAuthorize("@ss.hasPermi('system:division:export')")
    @ApiOperation(value = "行政区划列表", notes = "导出行政区划列表详情", tags = {"行政区划管理"})
    @Log(title = "行政区划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdministrativeDivision administrativeDivision) {
        List<AdministrativeDivision> list = administrativeDivisionService.selectAdministrativeDivisionList(administrativeDivision);
        ExcelUtil<AdministrativeDivision> util = new ExcelUtil<AdministrativeDivision>(AdministrativeDivision.class);
        util.exportExcel(response, list, "行政区划数据");
    }

    /**
     * 获取行政区划详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:division:query')")
    @ApiOperation(value = "行政区划详细信息", notes = "获取行政区划详细信息详情", tags = {"行政区划管理"})
    @GetMapping(value = "/{divisionId}")
    public AjaxResult getInfo(@PathVariable("divisionId") Long divisionId) {
        return success(administrativeDivisionService.selectAdministrativeDivisionByDivisionId(divisionId));
    }

    /**
     * 新增行政区划
     */
    //@PreAuthorize("@ss.hasPermi('system:division:add')")
    @ApiOperation(value = "新增行政区划", notes = "新增行政区划详情", tags = {"行政区划管理"})
    @Log(title = "行政区划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody AdministrativeDivision administrativeDivision) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        administrativeDivisionService.completeData(administrativeDivision, userId, 1);
        return toAjax(administrativeDivisionService.insertAdministrativeDivision(administrativeDivision));
    }

    /**
     * 修改行政区划
     */
    //@PreAuthorize("@ss.hasPermi('system:division:edit')")
    @ApiOperation(value = "修改行政区划", notes = "修改行政区划详情", tags = {"行政区划管理"})
    @Log(title = "行政区划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody AdministrativeDivision administrativeDivision) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        administrativeDivisionService.completeData(administrativeDivision, userId, 2);
        return toAjax(administrativeDivisionService.updateAdministrativeDivision(administrativeDivision));
    }

    /**
     * 删除行政区划
     */
    //@PreAuthorize("@ss.hasPermi('system:division:remove')")
    @ApiOperation(value = "删除行政区划", notes = "删除行政区划详情", tags = {"行政区划管理"})
    @Log(title = "行政区划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{divisionIds}")
    public AjaxResult remove(@PathVariable Long[] divisionIds) {
        return toAjax(administrativeDivisionService.deleteAdministrativeDivisionByDivisionIds(divisionIds));
    }
}
